package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    /**
     *新增菜品分类
     */
    @PostMapping
    public R<String> add(@RequestBody DishDto dishDto){

        dishService.saveWithFlavor(dishDto);

        Set<Object> keys = redisTemplate.keys("dishList_*");
        redisTemplate.delete(keys);
        return R.success("添加成功");
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public R<Page<DishDto>> page(Integer page,Integer pageSize,String name){

      Page<DishDto> pageInfo=  dishService.dishWishDishDto(page,pageSize,name);
      return R.success(pageInfo);
    }

    /**
     * 数据回显
     */
    @GetMapping("{id}")
    public R<DishDto> getById(@PathVariable Long id) {
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {
        dishService.updateWithFlavor(dishDto);
        Set<Object> keys = redisTemplate.keys("dishList_*");
        redisTemplate.delete(keys);
        return R.success("修改菜品信息成功");
    }

    /**
     * 根据分类查菜品
     */
    @GetMapping("/list")
    public R<List<DishDto>>  list(Dish dish){

        String redisKey="dishList_"+dish.getCategoryId();
        List<DishDto> redisList = (List<DishDto>) redisTemplate.opsForValue().get(redisKey);
        if(redisList !=null){
            return R.success(redisList);
        }
        LambdaQueryWrapper<Dish> queryWrapper = Wrappers.lambdaQuery(Dish.class)
                .eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId())
                .eq(Dish::getStatus, 1);
        List<Dish> dishList = dishService.list(queryWrapper);
        List<DishDto> dishDtoList = new ArrayList<>();
        for (Dish dis : dishList) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dis, dishDto);

            LambdaQueryWrapper<DishFlavor> wrapper = Wrappers.lambdaQuery(DishFlavor.class)
                    .eq(DishFlavor::getDishId, dis.getId());
            List<DishFlavor> dishFlavors = dishFlavorService.list(wrapper);

            dishDto.setFlavors(dishFlavors);
            dishDtoList.add(dishDto);
        }
        redisTemplate.opsForValue().set(redisKey,dishDtoList);
        return R.success(dishDtoList);
    }

    /**
     * 批量起售停售
     */
    @PostMapping("/status/{status}")
    @Transactional
    public R<String> starting(@PathVariable Integer status,Long[] ids){
        Dish dish = new Dish();
        dish.setStatus(status);
        for (Long id : ids) {
            dish.setId(id);
            dishService.updateById(dish);
        }
        Set<Object> keys = redisTemplate.keys("dishList_*");
        redisTemplate.delete(keys);
        return R.success("成功");
    }

    /**
     * 批量删除
     */
    @DeleteMapping
    public R<String> delete( Long[] ids) {

        dishService.deleteWithFlavor(ids);
        Set<Object> keys = redisTemplate.keys("dishList_*");
        redisTemplate.delete(keys);
        return R.success("删除成功");
    }
}
